package cn.sciento.farm.devicemanager.gateway.uplink.service.fertilizer.inject;

import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.dao.model.fertilizer.InjectFertilizer;
import cn.sciento.farm.devicemanager.gateway.model.uplink.fertilizer.inject.InjectFertilizerAck;
import cn.sciento.farm.devicemanager.gateway.model.uplink.fertilizer.inject.InjectFertilizerStatus;
import cn.sciento.farm.devicemanager.gateway.model.uplink.fertilizer.inject.InjectFertilizerSys;
import cn.sciento.farm.devicemanager.gateway.model.uplink.fertilizer.inject.InjectFertilizerWorkStatus;
import cn.sciento.farm.devicemanager.gateway.uplink.service.base.AbstractStringAdapter;
import cn.sciento.farm.devicemanager.gateway.uplink.service.fertilizer.InjectFertilizerDisposeService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service(value = MqttConstant.PROTOCOL_PREFIX + MqttConstant.PROTOCOL_INJECT_FEERTILIZER)
public class InjctFertilizerDataAdapter  extends AbstractStringAdapter {

    @Autowired
    private List<InjectFertilizerDisposeService> injectFertilizerDisposeServiceList;


    @Override
    public void analyseStringData(String[] subTopic, String message) {
        if(subTopic.length != 4){
            return;
        }
        String eui = subTopic[2];
        String messageType = subTopic[3];
        switch (messageType){
            case "deviceStatus":
                InjectFertilizerStatus status = JSON.parseObject(message,InjectFertilizerStatus.class);
                status.setEui(eui);
                for(InjectFertilizerDisposeService service :injectFertilizerDisposeServiceList){
                    service.handleStatus(status);
                }
                break;
            case "runningStatus":
                InjectFertilizerWorkStatus workStatus = JSON.parseObject(message,InjectFertilizerWorkStatus.class);
                workStatus.setEui(eui);
                for(InjectFertilizerDisposeService service :injectFertilizerDisposeServiceList){
                    service.handleWorkStatus(workStatus);
                }
                break;
            case "error":
                break;
            case "ack":
                InjectFertilizerAck ack = JSON.parseObject(message,InjectFertilizerAck.class);
                ack.setEui(eui);
                for(InjectFertilizerDisposeService service :injectFertilizerDisposeServiceList){
                    service.handleAck(ack);
                }
                break;
            case "sys":
                InjectFertilizerSys sys = JSON.parseObject(message,InjectFertilizerSys.class);
                sys.setEui(eui);
                for(InjectFertilizerDisposeService service :injectFertilizerDisposeServiceList){
                    service.handleSys(sys);
                }
                break;
            default:
                log.info("注入式水肥机上传非法的消息类型：{}",messageType);
        }
    }
}
